package com.hnhy.bluetooth.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hnhy.bluetooth.databinding.ItemPropertyBinding

class PropertyAdapter(
    private val property: List<String>,
    private val onItemClickListener: OnItemClickListener
) : RecyclerView.Adapter<PropertyAdapter.ViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(ItemPropertyBinding.inflate(LayoutInflater.from(parent.context), parent, false)).apply {
            binding.tvProperty.setOnClickListener { v -> onItemClickListener.onItemClick(v, adapterPosition) }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.tvProperty.text = property[position]
    }

    override fun getItemCount() = if (property.size > 0) property.size else 0

    class ViewHolder(itemView: ItemPropertyBinding) : RecyclerView.ViewHolder(itemView.root){
        val binding: ItemPropertyBinding
        init {
            this.binding = itemView
        }
    }

}